#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   font
@Time    :   2023/03/14 23:31:13
@author  :   Eval
@Version :   1.0
@Describe:   渲染游戏文本
'''
import pygame

class GameFont:
    def __init__(self,parent) -> None:
        self.parent = parent
        pygame.font.init()
        self.fonts = {
            "10":pygame.font.Font("lib/fonts/songti.TTF",10),
            "12":pygame.font.Font("lib/fonts/songti.TTF",12),
            "13":pygame.font.Font("lib/fonts/songti.TTF",13),
            "14":pygame.font.Font("lib/fonts/songti.TTF",14),
            "16":pygame.font.Font("lib/fonts/songti.TTF",16),
            "18":pygame.font.Font("lib/fonts/songti.TTF",18),
            "20":pygame.font.Font("lib/fonts/songti.TTF",20)
        }
    def render_lineText(self,x,y,text:str,fontSize:str="18",color=(255,255,255)):
        """渲染单行文本, 单个文字变色: 起始: «(255,0,0)»  结束:▣"""
        fontObj = self.fonts.get(str(fontSize))
        # wid = 0
        # for i in text:
            # 渲染阴影,防止字体模糊看不清楚
        self.parent.win.blit(self.getFontSurface(fontObj,text,(0,0,0)),(x+1,y))
        self.parent.win.blit(self.getFontSurface(fontObj,text,(0,0,0)),(x-1,y))
        self.parent.win.blit(self.getFontSurface(fontObj,text,(0,0,0)),(x,  y+1))
        self.parent.win.blit(self.getFontSurface(fontObj,text,(0,0,0)),(x,  y-1))
        self.parent.win.blit(self.getFontSurface(fontObj,text,color),(x,y))
        # wid += fontObj.size(i)[0]


    def render_multilineText(self,x,y,text:str,TWidth:int,fontSize:str="18",color=(255,255,255),indent=0):
        """渲染多行文本"""
        if indent == 0:
            indent = fontSize
        fontObj = self.fonts.get(str(fontSize))
        wid = 0
        hei = 0
        for i in text:
            self.parent.win.blit(self.getFontSurface(fontObj,i,(10,10,10)),(x+wid+1,y+hei))
            self.parent.win.blit(self.getFontSurface(fontObj,i,(10,10,10)),(x+wid-1,y+hei))
            self.parent.win.blit(self.getFontSurface(fontObj,i,(10,10,10)),(x+wid,  y+hei+1))
            self.parent.win.blit(self.getFontSurface(fontObj,i,(10,10,10)),(x+wid,  y+hei-1))
            self.parent.win.blit(self.getFontSurface(fontObj,i,color),(x+wid,y+hei))
            
            wid += fontObj.size(i)[0]
            if wid >= TWidth-int(indent):
                wid = 0
                hei += fontObj.size(i)[1]
            if i=="\n":
                wid = 0
                hei += fontObj.size(i)[1]
                continue
            
    def getFontSurface(self,fontObj,text,color:tuple=(255,255,255)):
        """获取字体渲染对象"""
        return fontObj.render(str(text),True,color)
    
    def getTextHeight(self,text,TWidth,fontSize:int=18,offst=10):
        """获取文本高度"""
        wid = offst
        hei = fontSize
        fontObj = self.fonts.get(str(fontSize))
        for i in text:
            wid += fontObj.size(i)[0]
            if wid >= TWidth-offst:
                wid = offst
                hei += fontObj.size(i)[1]
            if i=="\n":
                wid = offst
                hei += fontObj.size(i)[1]
                continue
        return hei
    
    
    def getTextWidth(self,text,fontSize:int=18):
        """获取文本宽度"""
        fontObj = self.fonts.get(str(fontSize))
        wid = 10
        for i in text:
            wid += fontObj.size(i)[0]
            # 只计算一行得宽度,换行直接结束计算
            if i=="\n":
                break
        return wid